package com.xuxin.pda.model;

import com.xuxin.pda.bean.LoginBean;
import com.xuxin.pda.contract.LoginContract;
import com.xuxin.pda.network.Retrofit;
import com.xuxin.pda.network.RetrofitApi;
import com.xuxin.pda.presenter.LoginPresenter;

import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
import io.reactivex.rxjava3.annotations.NonNull;
import io.reactivex.rxjava3.observers.DisposableObserver;
import io.reactivex.rxjava3.schedulers.Schedulers;

/**
 * Created by xu_xin
 * Created on 2020/12/24 0024
 */
public class LoginModel implements LoginContract.Model {
    @Override
    public void login(LoginPresenter presenter, String code) {
        Retrofit.getInstance().createReq(RetrofitApi.class)
                .login(code)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new DisposableObserver<LoginBean>() {
                    @Override
                    public void onNext(@NonNull LoginBean loginBean) {
                        if (loginBean.isSuccess()) {
                            presenter.onSuccess(loginBean);
                        } else {
                            presenter.onError(loginBean);
                        }
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        presenter.onError(null);
                    }

                    @Override
                    public void onComplete() {
                        presenter.onComplete();
                    }
                });
    }
}